package cn.jiguang.jgadpubadapter.adapter;

import static xyz.adscope.amps.common.AMPSErrorCode.ChannelErrorEnum.CHANNEL_ERROR_AD_CONTAINER_NULL;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;

import java.util.Map;

import cn.jiguang.jgadpubadapter.Constants;
import cn.jiguang.jgadpubadapter.JgPubAdInit;
import cn.jiguang.jgadpubadapter.LogUtil;
import cn.jiguang.jgssp.ad.ADJgBannerAd;
import cn.jiguang.jgssp.ad.data.ADJgAdInfo;
import cn.jiguang.jgssp.ad.error.ADJgError;
import cn.jiguang.jgssp.ad.listener.ADJgBannerAdListener;
import xyz.adscope.amps.ad.banner.adapter.AMPSBannerAdAdapterListener;
import xyz.adscope.amps.ad.banner.adapter.AMPSBannerAdapter;
import xyz.adscope.amps.base.AMPSBidResult;
import xyz.adscope.amps.common.AMPSConstants;
import xyz.adscope.amps.common.AMPSError;
import xyz.adscope.amps.init.inter.IAMPSChannelInitCallBack;
import xyz.adscope.amps.inner.AMPSAdBiddingListener;
import xyz.adscope.amps.model.AMPSAdapterModel;
import xyz.adscope.amps.tool.AMPSLogUtil;


public class JgPuAdBannerAdapter extends AMPSBannerAdapter {

    private ADJgBannerAd adJgBannerAd;
    private static final String TAG = "JgPubAdAdapter";

    @Override
    public void loadNetworkAd(Context context, AMPSAdapterModel params,
                              AMPSBannerAdAdapterListener listener) {
        LogUtil.d(TAG, "loadNetworkAd: ");
        super.loadNetworkAd(context, params, listener);
        //实现竞价逻辑时需要判断是否是bidding广告，如果是bidding广告，如果已经发起过请求直接回调广告请求成功
        if (isBidding && adJgBannerAd != null) {
            JgPuAdBannerAdapter.this.onAdLoad();
            return;
        }
        initSDK();
    }

    //实现竞价功能时，重写该方法,如果渠道sdk不支持竞价可以不用重写该方法
    @Override
    public void startBid(Context context, AMPSAdapterModel ampsAdapterModel,
                         AMPSAdBiddingListener listener) {
        super.startBid(context, ampsAdapterModel, listener);
        initSDK();
    }

    //实现竞价功能时，重写该方法,如果渠道sdk不支持竞价可以不用重写该方法，发送给渠道竞价成功通知
    @Override
    public void sendWinNotice(AMPSBidResult ampsBidResult) {
        super.sendWinNotice(ampsBidResult);
        //需要调用渠道sdk方法通知渠道竞胜成功，ampsBidResult内容详情参考文档
    }

    //实现竞价功能时，重写该方法,如果渠道sdk不支持竞价可以不用重写该方法，发送给渠道竞败通知
    @Override
    public void sendLossNotice(AMPSBidResult ampsBidResult) {
        super.sendLossNotice(ampsBidResult);
        //需要调用渠道sdk方法通知渠道竞败，ampsBidResult内容详情参考文档
    }

    @Override
    public void showAd(ViewGroup container) {
        if ( container == null) {
            JgPuAdBannerAdapter.this.onAdShowFailed(CHANNEL_ERROR_AD_CONTAINER_NULL.getErrorCode(),
                    CHANNEL_ERROR_AD_CONTAINER_NULL.getErrorMsg());
            return;
        }
//        container.addView(mBannerView);
    }

    @Override
    public boolean isValid() {
        if (adJgBannerAd == null) {
            return false;
        }
        return true;
    }

    @Override
    public void destroy() {
        super.destroy();
        adJgBannerAd = null;
    }

    /**
     * 初始化sdk
     */
    private void initSDK() {
        JgPubAdInit.getInstance().initSDK(mInitAdapterConfig, new IAMPSChannelInitCallBack() {
            @Override
            public void successCallBack() {
                LogUtil.d(TAG, "successCallBack: jgpub adapter");
                loadBannerAd();
            }

            @Override
            public void failCallBack(AMPSError ampsError) {
                LogUtil.d(TAG, " loadSplashAd onSplashLoadFail " +
                        "code:" + ampsError.getCode() + " message:" + ampsError.getMessage());
                JgPuAdBannerAdapter.this.onAdFailed(ampsError.getCode(), ampsError.getMessage());
            }
        });
    }

    /**
     * 请求广告
     */
    private void loadBannerAd() {
        View rootView = null;
        if (mContext instanceof Activity) {
            Window window = ((Activity) mContext).getWindow();
            if (window != null) {
                View view = window.getDecorView();
                if (view != null) {
                    rootView = view.getRootView();
                }
            }
        }
        if (!(rootView instanceof ViewGroup)) {
            JgPuAdBannerAdapter.this.onAdFailed(Constants.DEFAULT_ERROR_CODE,Constants.DEFAULT_ERROR_DESC);
            return;
        }

        adJgBannerAd = new ADJgBannerAd((Activity) mContext, (ViewGroup) rootView);
        adJgBannerAd.setListener(new ADJgBannerAdListener() {
            @Override
            public void onAdReceive(ADJgAdInfo adJgAdInfo) {
                LogUtil.d(TAG, "onAdReceive: "+adJgAdInfo.getPlatformPosId() );

                JgPuAdBannerAdapter.this.onAdLoad();
            }

            @Override
            public void onAdExpose(ADJgAdInfo adJgAdInfo) {
                LogUtil.d(TAG, "onAdExpose: "+adJgAdInfo.getPlatformPosId() );
            }

            @Override
            public void onAdClick(ADJgAdInfo adJgAdInfo) {
                LogUtil.d(TAG, "onAdClick: "+adJgAdInfo.getPlatformPosId() );
            }

            @Override
            public void onAdClose(ADJgAdInfo adJgAdInfo) {
                LogUtil.d(TAG, "onAdClose: "+adJgAdInfo.getPlatformPosId() );
            }

            @Override
            public void onAdFailed(ADJgError adJgError) {
                LogUtil.d(TAG, "onAdFailed: "+adJgError.toString() );
            }
        });
        LogUtil.d(TAG, "loadBannerAd: mSpaceId " + mSpaceId);

        adJgBannerAd.loadAd(mSpaceId);

    }


    @Override
    public Map<String, Object> getMediaExtraInfo() {
        return null;
    }

}
